package com.heiam.search.config;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@Getter
@Setter
@EnableAsync
@ConfigurationProperties(prefix = "leadnews.task.pool")
public class ThreadPoolConfig {

    //核心线程池数
    private int corePoolSize;
    //最大线程数
    private int maxPoolSize;
    //线程存活时间
    private int keepAliveSeconds;
    //任务队列
    private int queueCapacity;
    //线程池名前缀
    private String threadNamePrefix;

    @Bean("taskExecutor")
    public ThreadPoolTaskExecutor taskExecutor(){
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();

        taskExecutor.setCorePoolSize(corePoolSize);
        taskExecutor.setMaxPoolSize(maxPoolSize);
        taskExecutor.setKeepAliveSeconds(keepAliveSeconds);
        taskExecutor.setQueueCapacity(queueCapacity);
        taskExecutor.setThreadNamePrefix(threadNamePrefix);
        return taskExecutor;
    }

}
